-- CONFIG
APP_NAME = "unrealrealms"  -- important, change it, it's name for config dir and files in appdata
APP_VERSION = 1200       -- client version for updater and login to identify outdated client
DEFAULT_LAYOUT = "retro" -- on android it's forced to "mobile", check code bellow

-- If you don't use updater or other service, set it to updater = ""
Services = {
  website = "unrealrealms.com", -- currently not used
  updater = "https://panel.unrealrealms.com/api/updater.php",
  stats = "",
  crash = "http://otclient.ovh/api/crash.php",
  feedback = "http://otclient.ovh/api/feedback.php",
  status = "https://panel.unrealrealms.com/api/status.php"
}

-- Servers accept http login url, websocket login url or ip:port:version
Servers = {
  UnrealRealms = "unrealrealms.com:7171:1098"
}


--Server = "ws://otclient.ovh:3000/"
--Server = "ws://127.0.0.1:88/"
--USE_NEW_ENERGAME = true -- uses entergamev2 based on websockets instead of entergame
ALLOW_CUSTOM_SERVERS = true -- if true it shows option ANOTHER on server list

g_app.setName("Unreal Realms v1.2")
-- CONFIG END

-- print first terminal message
g_logger.info(os.date("== application started at %b %d %Y %X"))
g_logger.info(g_app.getName() .. ' ' .. g_app.getVersion() .. ' rev ' .. g_app.getBuildRevision() .. ' (' .. g_app.getBuildCommit() .. ') made by ' .. g_app.getAuthor() .. ' built on ' .. g_app.getBuildDate() .. ' for arch ' .. g_app.getBuildArch())

-- If data.zip exists, try to add it as a resources search path (for archive-based clients).
pcall(function()
  if g_resources and g_resources.fileExists and g_resources.addSearchPath then
    local candidates = { "/data.zip", "data.zip" }
    for _, path in ipairs(candidates) do
      if g_resources.fileExists(path) then
        g_resources.addSearchPath(path, true)
        break
      end
    end
  end
end)

-- Allow updater-downloaded files (stored in writeDir) to override base resources.
pcall(function()
  if g_resources and g_resources.getWriteDir and g_resources.addSearchPath then
    local wd = g_resources.getWriteDir()
    if wd and wd ~= "" then
      g_resources.addSearchPath(wd, true)
    end
  end
end)

if not g_resources.directoryExists("/data") then
  -- Allow running from data.zip (resources are not unpacked into folders)
  if not (g_resources.isLoadedFromArchive and g_resources.isLoadedFromArchive()) then
    local updaterEnabled = type(Services.updater) == 'string' and Services.updater:len() > 4
    if updaterEnabled then
      g_logger.warning("Data dir doesn't exist. Continuing to allow updater repair.")
    else
      g_logger.fatal("Data dir doesn't exist.")
    end
  end
end

if not g_resources.directoryExists("/modules") then
  -- Allow running from data.zip (resources are not unpacked into folders)
  if not (g_resources.isLoadedFromArchive and g_resources.isLoadedFromArchive()) then
    g_logger.fatal("Modules dir doesn't exist.")
  end
end

-- settings
g_configs.loadSettings("/config.otml")

-- set layout
local settings = g_configs.getSettings()
local layout = DEFAULT_LAYOUT
if g_app.isMobile() then
  layout = "mobile"
elseif settings:exists('layout') then
  layout = settings:getValue('layout')
end
g_resources.setLayout(layout)

-- load mods
g_modules.discoverModules()
g_modules.ensureModuleLoaded("corelib")
  
local function loadModules()
  -- libraries modules 0-99
  g_modules.autoLoadModules(99)
  g_modules.ensureModuleLoaded("gamelib")

  -- client modules 100-499
  g_modules.autoLoadModules(499)
  g_modules.ensureModuleLoaded("client")

  -- game modules 500-999
  g_modules.autoLoadModules(999)
  g_modules.ensureModuleLoaded("game_interface")
  -- Ensure affixes module is loaded (needs to be after game_skills and game_textwindow)
  g_modules.ensureModuleLoaded("game_affixes")
  -- Ensure reroll module is loaded (needs to be after game_textwindow)
  g_modules.ensureModuleLoaded("game_reroll")
  -- Ensure glory module is loaded (needs to be after game_textwindow)
  g_modules.ensureModuleLoaded("game_glory")
  -- Ensure global events module is loaded (extended opcode UI)
  g_modules.ensureModuleLoaded("game_global_events")

  -- mods 1000-9999
  g_modules.autoLoadModules(9999)
end

-- report crash
if type(Services.crash) == 'string' and Services.crash:len() > 4 and g_modules.getModule("crash_reporter") then
  g_modules.ensureModuleLoaded("crash_reporter")
end

-- run updater (allow updates even when resources are not in data.zip)
if type(Services.updater) == 'string' and Services.updater:len() > 4
  and g_modules.getModule("updater") then
  g_modules.ensureModuleLoaded("updater")
  return Updater.init(loadModules)
end
loadModules()
